THRESHOLD = (0, 60)
BINARY_VISIBLE = True
from pyb import UART
import json
import ustruct
import sensor, image, time
from pyb import LED
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 2000)
clock = time.clock()
uart = UART(3,115200)
uart.init(115200, bits=8, parity=None, stop=1)
green_led = LED(1)
while(True):
	clock.tick()
	green_led.on()
	img = sensor.snapshot().binary([THRESHOLD]) if BINARY_VISIBLE else sensor.snapshot()
	line = img.get_regression([(255,255) if BINARY_VISIBLE else THRESHOLD])
	if (line):
		img.draw_line(line.line(), color = 127)
		x=int(line[7])
		y=int(line[6])
		if(x<0):
			flag = 0
			x = -x
		else:
			flag = 2
			x =x
		data = bytearray([0xb3,x,flag,y,0x5b])
		uart.write(data)
		print(y)
uart.deinit()